home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / palette.h < prev    next >
C/C++ Source or Header  |  2000-04-30  |  12KB  |  228 lines

  1. /******************************************************************************
  2.  
  3.   palette.c
  4.  
  5.   Palette handling functions.
  6.  
  7.   There are several levels of abstraction in the way MAME handles the palette,
  8.   and several display modes which can be used by the drivers.
  9.  
  10.   Palette
  11.   -------
  12.   Note: in the following text, "color" refers to a color in the emulated
  13.   game's virtual palette. For example, a game might be able to display 1024
  14.   colors at the same time. If the game uses RAM to change the available
  15.   colors, the term "palette" refers to the colors available at any given time,
  16.   not to the whole range of colors which can be produced by the hardware. The
  17.   latter is referred to as "color space".
  18.   The term "pen" refers to one of the maximum MAX_PENS colors that can be
  19.   used to generate the display. PC users might want to think of them as the
  20.   colors available in VGA, but be warned: the mapping of MAME pens to the VGA
  21.   registers is not 1:1, so MAME's pen 10 will not necessarily be mapped to
  22.   VGA's color #10 (actually this is never the case). This is done to ensure
  23.   portability, since on some systems it is not possible to do a 1:1 mapping.
  24.  
  25.   So, to summarize, the three layers of palette abstraction are:
  26.  
  27.   P1) The game virtual palette (the "colors")
  28.   P2) MAME's MAX_PENS colors palette (the "pens")
  29.   P3) The OS specific hardware color registers (the "OS specific pens")
  30.  
  31.   The array Machine->pens is a lookup table which maps game colors to OS
  32.   specific pens (P1 to P3). When you are working on bitmaps at the pixel level,
  33.   *always* use Machine->pens to map the color numbers. *Never* use constants.
  34.   For example if you want to make pixel (x,y) of color 3, do:
  35.   bitmap->line[y][x] = Machine->pens[3];
  36.  
  37.  
  38.   Lookup table
  39.   ------------
  40.   Palettes are only half of the story. To map the gfx data to colors, the
  41.   graphics routines use a lookup table. For example if we have 4bpp tiles,
  42.   which can have 256 different color codes, the lookup table for them will have
  43.   256 * 2^4 = 4096 elements. For games using a palette RAM, the lookup table is
  44.   usually a 1:1 map. For games using PROMs, the lookup table is often larger
  45.   than the palette itself so for example the game can display 256 colors out
  46.   of a palette of 16.
  47.  
  48.   The palette and the lookup table are initialized to default values by the
  49.   main core, but can be initialized by the driver using the function
  50.   MachineDriver->vh_init_palette(). For games using palette RAM, that
  51.   function is usually not needed, and the lookup table can be set up by
  52.   properly initializing the color_codes_start and total_color_codes fields in
  53.   the GfxDecodeInfo array.
  54.   When vh_init_palette() initializes the lookup table, it maps gfx codes
  55.   to game colors (P1 above). The lookup table will be converted by the core to
  56.   map to OS specific pens (P3 above), and stored in Machine->remapped_colortable.
  57.  
  58.  
  59.   Display modes
  60.   -------------
  61.   The available display modes can be summarized in four categories:
  62.   1) Static palette. Use this for games which use PROMs for color generation.
  63.      The palette is initialized by vh_init_palette(), and never changed
  64.      again.
  65.   2) Dynamic palette. Use this for games which use RAM for color generation and
  66.      have no more than MAX_PENS colors in the palette. The palette can be
  67.      dynamically modified by the driver using the function
  68.      palette_change_color(). MachineDriver->video_attributes must contain the
  69.      flag VIDEO_MODIFIES_PALETTE.
  70.   3) Dynamic shrinked palette. Use this for games which use RAM for color
  71.      generation and have more than MAX_PENS colors in the palette. The palette
  72.      can be dynamically modified by the driver using the function
  73.      palette_change_color(). MachineDriver->video_attributes must contain the
  74.      flag VIDEO_MODIFIES_PALETTE.
  75.      The difference with case 2) above is that the driver must do some
  76.      additional work to allow for palette reduction without loss of quality.
  77.      The function palette_recalc() must be called every frame before doing any
  78.      rendering. The palette_used_colors array can be changed to precisely
  79.      indicate to the function which of the game colors are used, so it can pick
  80.      only the needed colors, and make the palette fit into MAX_PENS colors.
  81.      Colors can also be marked as "transparent".
  82.      The return code of palette_recalc() tells the driver whether the lookup
  83.      table has changed, and therefore whether a screen refresh is needed. Note
  84.      that this only applies to colors which were used in the previous frame:
  85.      that's why palette_recalc() must be called before ANY rendering takes
  86.      place.
  87.   4) 16-bit color. This should only be used for games which use more than
  88.      MAX_PENS colors at a time. It is slower than the other modes, so it should
  89.      be avoided whenever possible. Transparency support is limited.
  90.      MachineDriver->video_attributes must contain VIDEO_MODIFIES_PALETTE, and
  91.      GameDriver->flags must contain GAME_REQUIRES_16BIT.
  92.  
  93.   The dynamic shrinking of the palette works this way: as colors are requested,
  94.   they are associated to a pen. When a color is no longer needed, the pen is
  95.   freed and can be used for another color. When the code runs out of free pens,
  96.   it compacts the palette, putting together colors with the same RGB
  97.   components, then starts again to allocate pens for each new color. The bottom
  98.   line of all this is that the pen assignment will automatically adapt to the
  99.   game needs, and colors which change often will be assigned an exclusive pen,
  100.   which can be modified using the video cards hardware registers without need
  101.   for a screen refresh.
  102.   The important difference between cases 3) and 4) is that in 3), color cycling
  103.   (which many games use) is essentially free, while in 4) every palette change
  104.   requires a screen refresh. The color quality in 3) is also better than in 4)
  105.   if the game uses more than 5 bits per color component. For testing purposes,
  106.   you can switch between the two modes by just adding/removing the
  107.   GAME_REQUIRES_16BIT flag (but be warned about the limited transparency
  108.   support in 16-bit mode).
  109.  
  110. ******************************************************************************/
  111.  
  112. #ifndef PALETTE_H
  113. #define PALETTE_H
  114.  
  115. #define DYNAMIC_MAX_PENS 254    /* the Mac cannot handle more than 254 dynamic pens */
  116. #define STATIC_MAX_PENS 256        /* but 256 static pens can be handled */
  117.  
  118. int palette_start(void);
  119. void palette_stop(void);
  120. int palette_init(void);
  121.  
  122. void palette_change_color(int color,unsigned char red,unsigned char green,unsigned char blue);
  123.  
  124. /* This array is used by palette_recalc() to know which colors are used, and which */
  125. /* ones are transparent (see defines below). By default, it is initialized to */
  126. /* PALETTE_COLOR_USED for all colors; this is enough in some cases. */
  127. extern unsigned char *palette_used_colors;
  128.  
  129. void palette_increase_usage_count(int table_offset,unsigned int usage_mask,int color_flags);
  130. void palette_decrease_usage_count(int table_offset,unsigned int usage_mask,int color_flags);
  131. void palette_increase_usage_countx(int table_offset,int num_pens,const unsigned char *pen_data,int color_flags);
  132. void palette_decrease_usage_countx(int table_offset,int num_pens,const unsigned char *pen_data,int color_flags);
  133.  
  134. /* If you want to dynamically change the usage array, call palette_init_used_colors() */
  135. /* before setting used entries to PALETTE_COLOR_USED/PALETTE_COLOR_TRANSPARENT. */
  136. /* The function automatically marks colors used by the TileMap system. */
  137. void palette_init_used_colors(void);
  138.  
  139. const unsigned char *palette_recalc(void);
  140.  
  141. #define PALETTE_COLOR_UNUSED    0    /* This color is not needed for this frame */
  142. #define PALETTE_COLOR_VISIBLE    1    /* This color is currently visible */
  143. #define PALETTE_COLOR_CACHED    2    /* This color is cached in temporary bitmaps */
  144.     /* palette_recalc() will try to use always the same pens for the used colors; */
  145.     /* if it is forced to rearrange the pens, it will return TRUE to signal the */
  146.     /* driver that it must refresh the display. */
  147. #define PALETTE_COLOR_TRANSPARENT_FLAG    4    /* All colors using this attribute will be */
  148.     /* mapped to the same pen, and no other colors will be mapped to that pen. */
  149.     /* This way, transparencies can be handled by copybitmap(). */
  150.  
  151. /* backwards compatibility */
  152. #define PALETTE_COLOR_USED            (PALETTE_COLOR_VISIBLE|PALETTE_COLOR_CACHED)
  153. #define PALETTE_COLOR_TRANSPARENT    (PALETTE_COLOR_TRANSPARENT_FLAG|PALETTE_COLOR_USED)
  154.  
  155. /* if you use PALETTE_COLOR_TRANSPARENT, to do a transparency blit with copybitmap() */
  156. /* pass it TRANSPARENCY_PEN, palette_transparent_pen. */
  157. extern unsigned short palette_transparent_pen;
  158.  
  159. /* The transparent color can also be used as a background color, to avoid doing */
  160. /* a fillbitmap() + copybitmap() when there is nothing between the background */
  161. /* and the transparent layer. By default, the background color is black; you */
  162. /* can change it by doing: */
  163. /* palette_change_color(palette_transparent_color,R,G,B); */
  164. /* by default, palette_transparent_color is -1; you are allowed to change it */
  165. /* to make it point to a color in the game's palette, so the background color */
  166. /* can automatically handled by your paletteram_w() function. The Tecmo games */
  167. /* do this. */
  168. extern int palette_transparent_color;
  169.  
  170.  
  171. extern unsigned short *palette_shadow_table;
  172. extern unsigned short *palette_highlight_table;
  173.  
  174.  
  175.  
  176. /* here some functions to handle commonly used palette layouts, so you don't have */
  177. /* to write your own paletteram_w() function. */
  178.  
  179. extern unsigned char *paletteram;
  180. extern unsigned char *paletteram_2;    /* use when palette RAM is split in two parts */
  181.  
  182. READ_HANDLER( paletteram_r );
  183. READ_HANDLER( paletteram_2_r );
  184. READ_HANDLER( paletteram_word_r );    /* for 16 bit CPU */
  185. READ_HANDLER( paletteram_2_word_r );    /* for 16 bit CPU */
  186.  
  187. WRITE_HANDLER( paletteram_BBGGGRRR_w );
  188. WRITE_HANDLER( paletteram_RRRGGGBB_w );
  189. WRITE_HANDLER( paletteram_IIBBGGRR_w );
  190. WRITE_HANDLER( paletteram_BBGGRRII_w );
  191.  
  192. /* _w       least significant byte first */
  193. /* _swap_w  most significant byte first */
  194. /* _split_w least and most significant bytes are not consecutive */
  195. /* _word_w  use with 16 bit CPU */
  196. /*                        MSB          LSB */
  197. WRITE_HANDLER( paletteram_xxxxBBBBGGGGRRRR_w );
  198. WRITE_HANDLER( paletteram_xxxxBBBBGGGGRRRR_swap_w );
  199. WRITE_HANDLER( paletteram_xxxxBBBBGGGGRRRR_split1_w );    /* uses paletteram[] */
  200. WRITE_HANDLER( paletteram_xxxxBBBBGGGGRRRR_split2_w );    /* uses paletteram_2[] */
  201. WRITE_HANDLER( paletteram_xxxxBBBBGGGGRRRR_word_w );
  202. WRITE_HANDLER( paletteram_xxxxBBBBRRRRGGGG_w );
  203. WRITE_HANDLER( paletteram_xxxxBBBBRRRRGGGG_swap_w );
  204. WRITE_HANDLER( paletteram_xxxxBBBBRRRRGGGG_split1_w );    /* uses paletteram[] */
  205. WRITE_HANDLER( paletteram_xxxxBBBBRRRRGGGG_split2_w );    /* uses paletteram_2[] */
  206. WRITE_HANDLER( paletteram_xxxxRRRRBBBBGGGG_split1_w );    /* uses paletteram[] */
  207. WRITE_HANDLER( paletteram_xxxxRRRRBBBBGGGG_split2_w );    /* uses paletteram_2[] */
  208. WRITE_HANDLER( paletteram_xxxxRRRRGGGGBBBB_w );
  209. WRITE_HANDLER( paletteram_xxxxRRRRGGGGBBBB_word_w );
  210. WRITE_HANDLER( paletteram_RRRRGGGGBBBBxxxx_swap_w );
  211. WRITE_HANDLER( paletteram_RRRRGGGGBBBBxxxx_split1_w );    /* uses paletteram[] */
  212. WRITE_HANDLER( paletteram_RRRRGGGGBBBBxxxx_split2_w );    /* uses paletteram_2[] */
  213. WRITE_HANDLER( paletteram_RRRRGGGGBBBBxxxx_word_w );
  214. WRITE_HANDLER( paletteram_BBBBGGGGRRRRxxxx_swap_w );
  215. WRITE_HANDLER( paletteram_BBBBGGGGRRRRxxxx_split1_w );    /* uses paletteram[] */
  216. WRITE_HANDLER( paletteram_BBBBGGGGRRRRxxxx_split2_w );    /* uses paletteram_2[] */
  217. WRITE_HANDLER( paletteram_BBBBGGGGRRRRxxxx_word_w );
  218. WRITE_HANDLER( paletteram_xBBBBBGGGGGRRRRR_w );
  219. WRITE_HANDLER( paletteram_xBBBBBGGGGGRRRRR_swap_w );
  220. WRITE_HANDLER( paletteram_xBBBBBGGGGGRRRRR_word_w );
  221. WRITE_HANDLER( paletteram_xRRRRRGGGGGBBBBB_w );
  222. WRITE_HANDLER( paletteram_xRRRRRGGGGGBBBBB_word_w );
  223. WRITE_HANDLER( paletteram_xGGGGGRRRRRBBBBB_word_w );
  224. WRITE_HANDLER( paletteram_IIIIRRRRGGGGBBBB_word_w );
  225. WRITE_HANDLER( paletteram_RRRRGGGGBBBBIIII_word_w );
  226.  
  227. #endif
  228.